"""
pytest 配置文件和共享夹具

此文件包含了测试的全局配置和常用的测试夹具。
"""

import pytest
import tempfile
import os
from pathlib import Path
from unittest.mock import MagicMock

from minicoder.utils.config import Config


@pytest.fixture
def temp_dir():
    """创建临时目录的夹具"""
    with tempfile.TemporaryDirectory() as tmpdir:
        yield Path(tmpdir)


@pytest.fixture
def mock_config(temp_dir):
    """创建模拟配置的夹具"""
    config_file = temp_dir / "test_config.yaml"
    config_content = """
default:
  provider: openrouter
  model: openai/gpt-3.5-turbo
  max_tokens: 2048

providers:
  openrouter:
    api_key: test_key
    base_url: https://openrouter.ai/api/v1
    models:
      - openai/gpt-3.5-turbo
      - openai/gpt-4
  
  ollama:
    base_url: http://localhost:11434
    models:
      - llama2
      - codellama
      
  siliconflow:
    api_key: test_silicon_key
    base_url: https://api.siliconflow.cn/v1
    models:
      - Qwen/Qwen2.5-7B-Instruct
"""
    config_file.write_text(config_content)
    
    config = Config(config_file)
    return config


@pytest.fixture
def mock_ai_provider():
    """创建模拟 AI 提供商的夹具"""
    provider = MagicMock()
    provider.generate_response.return_value = "Mock AI response"
    provider.stream_response.return_value = iter(["Mock ", "streaming ", "response"])
    return provider


@pytest.fixture(autouse=True)
def setup_test_env(monkeypatch):
    """自动设置测试环境变量"""
    # 设置测试环境变量
    monkeypatch.setenv("MINICODER_TEST_MODE", "true")
    # 禁用网络请求（如果需要的话）
    # monkeypatch.setenv("MINICODER_OFFLINE_MODE", "true")
